home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / diff.zip / IO.C < prev    next >
Text File  |  1994-07-16  |  22KB  |  748 lines

  1. /* File I/O for GNU DIFF.
  2.    Copyright (C) 1988, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU DIFF is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU DIFF; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  21. This port is also distributed under the terms of the GNU General
  22. Public License as published by the Free Software Foundation.
  23.  
  24. Please note that this file is not identical to the original GNU release,
  25. you should have received this code as patch to the official release.
  26.  
  27.    Friday, 15 July 1994  Troy Rollo (troy@cbme.unsw.edu.au)
  28.  
  29.     - Removed "slurp entire file at once" case, and
  30.       changed xhalloc, xhrealloc, and hread to reasonable alternatives.
  31.  
  32. $Header: e:/gnu/diff/RCS/io.c 1.15.0.2 91/03/12 17:06:38 tho Exp $  */
  33.  
  34. #include "diff.h"
  35.  
  36. #ifdef __STDC__
  37. static  int binary_file_p(char *, int);
  38. static  int slurp(void);
  39. static  void find_identical_ends(struct file_data *);
  40. static  void find_and_hash_each_line(void);
  41. static  int find_equiv_class(int);
  42. #endif /* __STDC__ */
  43.  
  44. /* Rotate a value n bits to the left. */
  45. #define UINT_BIT (sizeof (unsigned) * CHAR_BIT)
  46. #define ROL(v, n) ((v) << (n) | (v) >> UINT_BIT - (n))
  47.  
  48. /* Given a hash value and a new character, return a new hash value. */
  49. #define HASH(h, c) ((c) + ROL (h, 7))
  50.  
  51. /* Current file under consideration. */
  52. struct file_data *current;
  53.  
  54. /* Check for binary files and compare them for exact identity.  */
  55.  
  56. /* Return 1 if BUF contains a non text character.
  57.    SIZE is the number of characters in BUF.  */
  58.  
  59. static int
  60. binary_file_p (buf, size)
  61.      char *buf;
  62.      int size;
  63. {
  64.   static const char textchar[] = {
  65.     /* ISO 8859 */
  66.     0, 0, 0, 0, 0, 0, 0, 0,
  67.     0, 1, 1, 1, 1, 1, 0, 0,
  68.     0, 0, 0, 0, 0, 0, 0, 0,
  69.     0, 0, 0, 0, 0, 0, 0, 0,
  70.     1, 1, 1, 1, 1, 1, 1, 1,
  71.     1, 1, 1, 1, 1, 1, 1, 1,
  72.     1, 1, 1, 1, 1, 1, 1, 1,
  73.     1, 1, 1, 1, 1, 1, 1, 1,
  74.     1, 1, 1, 1, 1, 1, 1, 1,
  75.     1, 1, 1, 1, 1, 1, 1, 1,
  76.     1, 1, 1, 1, 1, 1, 1, 1,
  77.     1, 1, 1, 1, 1, 1, 1, 1,
  78.     1, 1, 1, 1, 1, 1, 1, 1,
  79.     1, 1, 1, 1, 1, 1, 1, 1,
  80.     1, 1, 1, 1, 1, 1, 1, 1,
  81.     1, 1, 1, 1, 1, 1, 1, 0,
  82.     0, 0, 0, 0, 0, 0, 0, 0,
  83.     0, 0, 0, 0, 0, 0, 0, 0,
  84.     0, 0, 0, 0, 0, 0, 0, 0,
  85.     0, 0, 0, 0, 0, 0, 0, 0,
  86.     1, 1, 1, 1, 1, 1, 1, 1,
  87.     1, 1, 1, 1, 1, 1, 1, 1,
  88.     1, 1, 1, 1, 1, 1, 1, 1,
  89.     1, 1, 1, 1, 1, 1, 1, 1,
  90.     1, 1, 1, 1, 1, 1, 1, 1,
  91.     1, 1, 1, 1, 1, 1, 1, 1,
  92.     1, 1, 1, 1, 1, 1, 1, 1,
  93.     1, 1, 1, 1, 1, 1, 1, 1,
  94.     1, 1, 1, 1, 1, 1, 1, 1,
  95.     1, 1, 1, 1, 1, 1, 1, 1,
  96.     1, 1, 1, 1, 1, 1, 1, 1,
  97.     1, 1, 1, 1, 1, 1, 1, 1,
  98.   };
  99.   while (--size >= 0)
  100.     if (!textchar[*buf++ & 0377])
  101.       return 1;
  102.   return 0;
  103. }
  104.  
  105. int binary_file_threshold = 512;
  106.  
  107. /* Slurp the current file completely into core.
  108.    Return nonzero if it appears to be a binary file.  */
  109.  
  110. static int
  111. slurp ()
  112. {
  113.   /* If we have a nonexistent file at this stage, treat it as empty.  */
  114.   if (current->desc < 0)
  115.     {
  116.       current->bufsize = 0;
  117.       current->buffered_chars = 0;
  118.       current->buffer = 0;
  119.     }
  120.   /* In all cases, we leave room in the buffer for 2 extra chars
  121.      beyond those that current->bufsize describes:
  122.      one for a newline (in case the text does not end with one)
  123.      and one for a sentinel in find_identical_ends.  */
  124.   else
  125.     {
  126. #ifdef MSDOS
  127.       long cc;
  128.  
  129.       current->bufsize = 0x2000L;
  130.       current->buffer = (char _huge *) farmalloc (current->bufsize + 2L);
  131.       current->buffered_chars = 0L;
  132.  
  133.       /* Not a regular file; read it in a little at a time, growing the
  134.      buffer as necessary.
  135.      MS-DOS: This is really slow size we do not double
  136.      the BUFSIZE on each step, we rather increase it linearly.
  137.      I think this greatly inproves the changes of managing a 
  138.      tight fit.  */
  139.       while ((cc = read (current->desc,
  140.                current->buffer + current->buffered_chars,
  141.                current->bufsize - current->buffered_chars))
  142.           > 0L)
  143.     {
  144.       current->buffered_chars += cc;
  145.       if (current->buffered_chars == current->bufsize)
  146.         {
  147.           current->buffer
  148.         = (char _huge *) farrealloc (current->buffer,
  149.                        current->bufsize + 0x2000L);
  150.           current->bufsize = current->bufsize + 0x2000L;
  151.         }
  152.     }
  153.  
  154.       if (cc < 0L)
  155.     pfatal_with_name (current->name);
  156.  
  157. #else /* not MSDOS */
  158.  
  159.       int cc;
  160.  
  161.       current->bufsize = 4096;
  162.       current->buffer = (char *) xmalloc (current->bufsize + 2);
  163.       current->buffered_chars = 0;
  164.  
  165.       /* Not a regular file; read it in a little at a time, growing the
  166.      buffer as necessary. */
  167.       while ((cc = read (current->desc,
  168.              current->buffer + current->buffered_chars,
  169.              current->bufsize - current->buffered_chars))
  170.          > 0)
  171.     {
  172.       current->buffered_chars += cc;
  173.       if (current->buffered_chars == current->bufsize)
  174.         {
  175.           current->bufsize = current->bufsize * 2;
  176.           current->buffer = (char *) xrealloc (current->buffer,
  177.                            current->bufsize + 2);
  178.         }
  179.     }
  180.  
  181.       if (cc < 0)
  182.     pfatal_with_name (current->name);
  183.  
  184. #endif /* not MSDOS */
  185.     }
  186.  
  187.   /* Check first part of file to see if it's a binary file.  */
  188.   if (! always_text_flag
  189.       && binary_file_p (current->buffer,
  190. #ifdef MSDOS
  191.        min ((int) current->buffered_chars, binary_file_threshold)))
  192. #else
  193.             min (current->buffered_chars, binary_file_threshold)))
  194. #endif
  195.     return 1;
  196.  
  197.   /* If not binary, make sure text ends in a newline,
  198.      but remember that we had to add one unless -B is in effect.  */
  199.   if (current->buffered_chars > 0
  200.       && current->buffer[current->buffered_chars - 1] != '\n')
  201.     {
  202.       current->missing_newline = !ignore_blank_lines_flag;
  203.       current->buffer[current->buffered_chars++] = '\n';
  204.     }
  205.   else
  206.     current->missing_newline = 0;
  207.  
  208.   /* Don't use uninitialized storage. */
  209.   if (current->buffer != 0)
  210.     current->buffer[current->buffered_chars] = '\0';
  211.  
  212.   return 0;
  213. }
  214.  
  215. /* Split the file into lines, simultaneously computing the hash codes for
  216.    each line. */
  217.  
  218. void
  219. find_and_hash_each_line ()
  220. {
  221.   unsigned h;
  222.   int i;
  223.   unsigned char _huge *p = (unsigned char _huge *) current->prefix_end;
  224.   unsigned char _huge *ip, c;
  225.  
  226.   /* Attempt to get a good initial guess as to the number of lines. */
  227. #ifdef MSDOS
  228.   current->linbufsize = (int) (current->buffered_chars / 50) + 5;
  229. #else
  230.   current->linbufsize = current->buffered_chars / 50 + 5;
  231. #endif
  232.   current->linbuf
  233.     = (struct line_def *) xmalloc (current->linbufsize * sizeof (struct line_def));
  234.  
  235.   if (function_regexp || output_style == OUTPUT_IFDEF)
  236.     {
  237.       /* If the -C, -D or -F option is used, we need to find the lines
  238.      of the matching prefix.  At least we will need to find the last few,
  239.      but since we don't know how many, it's easiest to find them all.
  240.      If -D is specified, we need all the lines of the first file.  */
  241.       current->buffered_lines = 0;
  242.       p = (unsigned char _huge *) current->buffer;
  243.     }
  244.   else
  245.     {
  246.       /* Skip the identical prefixes, except be prepared to handle context.
  247.      In fact, handle 1 more preceding line than the context says,
  248.      in case shift_boundaries moves things backwards in this file.  */
  249.       current->buffered_lines = current->prefix_lines - context - 1;
  250.       if (current->buffered_lines < 0)
  251.     current->buffered_lines = 0;
  252.       for (i = 0; i < context + 1; ++i)
  253.     /* Unless we are at the beginning, */
  254.     if ((char _huge *) p != current->buffer)
  255.       /* Back up at least 1 char until at the start of a line.  */
  256.       while ((char _huge *) --p != current->buffer && p[-1] != '\n')
  257.         ;
  258.     }
  259.  
  260.   while ((char _huge *) p < current->suffix_begin)
  261.     {
  262.       h = 0;
  263.       ip = p;
  264.  
  265.       if (current->prefix_end <= (char _huge *) p)
  266.     {
  267.       /* Hash this line until we find a newline. */
  268.       if (ignore_case_flag)
  269.         {
  270.           if (ignore_all_space_flag)
  271.         while ((c = *p) != '\n')
  272.           {
  273.             if (! isspace (c))
  274.               if (isupper (c))
  275.             h = HASH (h, tolower (c));
  276.               else
  277.             h = HASH (h, c);
  278.             ++p;
  279.           }
  280.           else if (ignore_space_change_flag)
  281.  
  282.         while ((c = *p) != '\n')
  283.           {
  284.             if (c == ' ' || c == '\t')
  285.               {
  286.             while ((c = *p) == ' ' || c == '\t')
  287.               ++p;
  288.             if (c == '\n')
  289.               break;
  290.             h = HASH (h, ' ');
  291.               }
  292.             /* C is now the first non-space.  */
  293.             if (isupper (c))
  294.               h = HASH (h, tolower (c));
  295.             else
  296.               h = HASH (h, c);
  297.             ++p;
  298.           }
  299.           else
  300.         while ((c = *p) != '\n')
  301.           {
  302.             if (isupper (c))
  303.               h = HASH (h, tolower (c));
  304.             else
  305.               h = HASH (h, c);
  306.             ++p;
  307.           }
  308.         }
  309.       else
  310.         {
  311.           if (ignore_all_space_flag)
  312.         while ((c = *p) != '\n')
  313.           {
  314.             if (! isspace (c))
  315.               h = HASH (h, c);
  316.             ++p;
  317.           }
  318.           else if (ignore_space_change_flag)
  319.         while ((c = *p) != '\n')
  320.           {
  321.             if (c == ' ' || c == '\t')
  322.               {
  323.             while ((c = *p) == ' ' || c == '\t')
  324.               ++p;
  325.             if (c == '\n')
  326.               break;
  327.             h = HASH (h, ' ');
  328.               }
  329.             /* C is not the first non-space.  */
  330.             h = HASH (h, c);
  331.             ++p;
  332.           }
  333.           else
  334.         while ((c = *p) != '\n')
  335.           {
  336.             h = HASH (h, c);
  337.             ++p;
  338.           }
  339.         }
  340.     }
  341.       else
  342.     /* This line is part of the matching prefix,
  343.        so we don't need to hash it.  */
  344.     while (*p != '\n')
  345.       ++p;
  346.       
  347.       /* Maybe increase the size of the line table. */
  348.       if (current->buffered_lines >= current->linbufsize)
  349.     {
  350.       while (current->buffered_lines >= current->linbufsize)
  351. #ifdef MSDOS            /* don't be to generous!  */
  352.         {
  353.           current->linbufsize += 0x0100;
  354.           if (current->linbufsize >= 0xffe0 / sizeof (struct line_def))
  355.         fatal ("to many lines in input");
  356.         }
  357. #else /* not MSDOS */
  358.         current->linbufsize *= 2;
  359. #endif /* not MSDOS */
  360.       current->linbuf
  361.         = (struct line_def *) xrealloc (current->linbuf,
  362.                         current->linbufsize
  363.                         * sizeof (struct line_def));
  364.     }
  365.       current->linbuf[current->buffered_lines].text = (char _huge *) ip;
  366. #ifdef MSDOS            /* Be explicit to the compiler!  */
  367.       current->linbuf[current->buffered_lines].length
  368.     = (int) ((long) (p - ip) + 1);
  369. #else /* not MSDOS */
  370.       current->linbuf[current->buffered_lines].length = p - ip + 1;
  371. #endif /* not MSDOS */
  372.       current->linbuf[current->buffered_lines].hash = h;
  373.       ++current->buffered_lines;
  374.       ++p;
  375.     }
  376.  
  377.   i = 0;
  378.   while ((i < context || output_style == OUTPUT_IFDEF)
  379.      && (char _huge *) p < current->buffer + current->buffered_chars)
  380.     {
  381.       ip = p;
  382.       while (*p++ != '\n')
  383.     ;
  384.       /* Maybe increase the size of the line table. */
  385.       if (current->buffered_lines >= current->linbufsize)
  386.     {
  387.       while (current->buffered_lines >= current->linbufsize)
  388. #ifdef MSDOS            /* don't be to generous!  */
  389.         {
  390.           current->linbufsize += 0x0100;
  391.           if (current->linbufsize >= 0xffe0 / sizeof (struct line_def))
  392.         fatal ("to many lines in input");
  393.         }
  394. #else /* not MSDOS */
  395.         current->linbufsize *= 2;
  396. #endif /* not MSDOS */
  397.       current->linbuf
  398.         = (struct line_def *) xrealloc (current->linbuf,
  399.                         current->linbufsize
  400.                         * sizeof (struct line_def));
  401.     }
  402.       current->linbuf[current->buffered_lines].text = (char _huge *) ip;
  403. #ifdef MSDOS        /* Be explicit to the compiler!  */
  404.       current->linbuf[current->buffered_lines].length
  405.     = (int) ((long) (p - ip));
  406. #else /* not MSDOS */
  407.       current->linbuf[current->buffered_lines].length = p - ip;
  408. #endif /* not MSDOS */
  409.       current->linbuf[current->buffered_lines].hash = 0;
  410.       ++current->buffered_lines;
  411.       ++i;
  412.     }
  413.  
  414.   if (ROBUST_OUTPUT_STYLE (output_style)
  415.       && current->missing_newline
  416.       && current->suffix_begin == current->buffer + current->buffered_chars)
  417.     --current->linbuf[current->buffered_lines - 1].length;
  418. }
  419.  
  420. /* Given a vector of two file_data objects, find the identical
  421.    prefixes and suffixes of each object. */
  422.  
  423. static void
  424. find_identical_ends (filevec)
  425.      struct file_data filevec[];
  426. {
  427.   char _huge *p0, _huge *p1, _huge *end0, _huge *beg0;
  428.   int lines;
  429.  
  430.   if (filevec[0].buffered_chars == 0 || filevec[1].buffered_chars == 0)
  431.     {
  432.       filevec[0].prefix_end = filevec[0].buffer;
  433.       filevec[1].prefix_end = filevec[1].buffer;
  434.       filevec[0].prefix_lines = filevec[1].prefix_lines = 0;
  435.       filevec[0].suffix_begin = filevec[0].buffer + filevec[0].buffered_chars;
  436.       filevec[1].suffix_begin = filevec[1].buffer + filevec[1].buffered_chars;
  437.       filevec[0].suffix_lines = filevec[1].suffix_lines = 0;
  438.       return;
  439.     }
  440.  
  441.   /* Find identical prefix.  */
  442.  
  443.   p0 = filevec[0].buffer;
  444.   p1 = filevec[1].buffer;
  445.   lines = 0;
  446.  
  447.   /* Insert end "sentinels", in this case characters that are guaranteed
  448.      to make the equality test false, and thus terminate the loop.  */
  449.  
  450.   if (filevec[0].buffered_chars < filevec[1].buffered_chars)
  451.     p0[filevec[0].buffered_chars] = ~p1[filevec[0].buffered_chars];
  452.   else
  453.     p1[filevec[1].buffered_chars] = ~p0[filevec[1].buffered_chars];
  454.  
  455.   /* Loop until first mismatch, or to the sentinel characters.  */
  456.   while (1)
  457.     {
  458.       char c = *p0++;
  459.       if (c != *p1++)
  460.     break;
  461.       if (c == '\n')
  462.     ++lines;
  463.     }
  464.  
  465.   /* Don't count missing newline as part of prefix in RCS mode. */
  466.   if (ROBUST_OUTPUT_STYLE (output_style)
  467. #ifdef MSDOS
  468.       && ((filevec[0].missing_newline
  469.        && (long) (p0 - filevec[0].buffer)
  470.          > (long) filevec[0].buffered_chars)
  471.       ||
  472.       (filevec[1].missing_newline
  473.        && (long) (p1 - filevec[1].buffer)
  474.          > (long) filevec[1].buffered_chars)))
  475. #else /* not MSDOS */
  476.       && ((filevec[0].missing_newline
  477.        && p0 - filevec[0].buffer > filevec[0].buffered_chars)
  478.       ||
  479.       (filevec[1].missing_newline
  480.        && p1 - filevec[1].buffer > filevec[1].buffered_chars)))
  481. #endif /* not MSDOS */
  482.     --p0, --p1, --lines;
  483.  
  484.   /* If the sentinel was passed, and lengths are equal, the
  485.      files are identical.  */
  486. #ifdef MSDOS
  487.   if ((long) (p0 - filevec[0].buffer) > (long) filevec[0].buffered_chars
  488. #else /* not MSDOS */
  489.   if (p0 - filevec[0].buffer > filevec[0].buffered_chars
  490. #endif /* not MSDOS */
  491.       && filevec[0].buffered_chars == filevec[1].buffered_chars)
  492.     {
  493.       filevec[0].prefix_end = p0 - 1;
  494.       filevec[1].prefix_end = p1 - 1;
  495.       filevec[0].prefix_lines = filevec[1].prefix_lines = lines;
  496.       filevec[0].suffix_begin = filevec[0].buffer;
  497.       filevec[1].suffix_begin = filevec[1].buffer;
  498.       filevec[0].suffix_lines = filevec[1].suffix_lines = lines;
  499.       return;
  500.     }
  501.  
  502.   /* Point at first nonmatching characters.  */
  503.   --p0, --p1;
  504.  
  505.   /* Skip back to last line-beginning in the prefix.  */
  506.   while (p0 != filevec[0].buffer && p0[-1] != '\n')
  507.     --p0, --p1;
  508.  
  509.   /* Record the prefix.  */
  510.   filevec[0].prefix_end = p0;
  511.   filevec[1].prefix_end = p1;
  512.   filevec[0].prefix_lines = filevec[1].prefix_lines = lines;
  513.   
  514.   /* Find identical suffix.  */
  515.  
  516.   /* P0 and P1 point beyond the last chars not yet compared.  */
  517.   p0 = filevec[0].buffer + filevec[0].buffered_chars;
  518.   p1 = filevec[1].buffer + filevec[1].buffered_chars;
  519.   lines = 0;
  520.  
  521.   if (! ROBUST_OUTPUT_STYLE (output_style)
  522.       || filevec[0].missing_newline == filevec[1].missing_newline)
  523.     {
  524.       end0 = p0;        /* Addr of last char in file 0.  */
  525.  
  526.       /* Get value of P0 at which we should stop scanning backward:
  527.      this is when either P0 or P1 points just past the last char
  528.      of the identical prefix.  */
  529.       if (filevec[0].buffered_chars < filevec[1].buffered_chars)
  530.     beg0 = filevec[0].prefix_end;
  531.       else
  532.     /* Figure out where P0 will be when P1 is at the end of the prefix.
  533.        Thus we only need to test P0.  */
  534.     beg0 = (filevec[0].prefix_end
  535.         + filevec[0].buffered_chars - filevec[1].buffered_chars);
  536.  
  537.       /* Scan back until chars don't match or we reach that point.  */
  538.       while (p0 != beg0)
  539.     {
  540.       char c = *--p0;
  541.       if (c != *--p1)
  542.         {
  543.           /* Point at the first char of the matching suffix.  */
  544.           ++p0, ++p1;
  545.           break;
  546.         }
  547.       if (c == '\n')
  548.         ++lines;
  549.     }
  550.  
  551.       /* Are we at a line-beginning in both files?  */
  552.       if (p0 != end0
  553.       && !((p0 == filevec[0].buffer || p0[-1] == '\n')
  554.            &&
  555.            (p1 == filevec[1].buffer || p1[-1] == '\n')))
  556.     {
  557.       /* No.  We counted one line too many.  */
  558.       --lines;
  559.       /* Advance to next place that is a line-beginning in both files.  */
  560.       do
  561.         {
  562.           ++p0, ++p1;
  563.         }
  564.       while (p0 != end0 && p0[-1] != '\n');
  565.     }
  566.     }
  567.  
  568.   /* Record the suffix.  */
  569.   filevec[0].suffix_begin = p0;
  570.   filevec[1].suffix_begin = p1;
  571.   filevec[0].suffix_lines = filevec[1].suffix_lines = lines;
  572. }
  573.  
  574. /* Lines are put into equivalence classes (of lines that match in line_cmp).
  575.    Each equivalence class is represented by one of these structures,
  576.    but only while the classes are being computed.
  577.    Afterward, each class is represented by a number.  */
  578. struct equivclass
  579. {
  580.   struct equivclass *next;    /* Next item in this bucket. */
  581.   struct line_def line;    /* A line that fits this class. */
  582. };
  583.  
  584. /* Hash-table: array of buckets, each being a chain of equivalence classes.  */
  585. static struct equivclass **buckets;
  586.   
  587. /* Size of the bucket array. */
  588. static int nbuckets;
  589.  
  590. /* Array in which the equivalence classes are allocated.
  591.    The bucket-chains go through the elements in this array.
  592.    The number of an equivalence class is its index in this array.  */
  593. static struct equivclass *equivs;
  594.  
  595. /* Index of first free element in the array `equivs'.  */
  596. static int equivs_index;
  597.  
  598. /* Size allocated to the array `equivs'.  */
  599. static int equivs_alloc;
  600.  
  601. /* Largest primes less than some power of two, for nbuckets.  Values range
  602.    from useful to preposterous.  If one of these numbers isn't prime
  603.    after all, don't blame it on me, blame it on primes (6) . . . */
  604. static int primes[] =
  605. {
  606.   509,
  607.   1021,
  608.   2039,
  609.   4093,
  610.   8191,
  611.   16381,
  612.   32749,
  613.   65521,
  614. #ifndef MSDOS
  615.   131071,
  616.   262139,
  617.   524287,
  618.   1048573,
  619.   2097143,
  620.   4194301,
  621.   8388593,
  622.   16777213,
  623.   33554393,
  624.   67108859,            /* Preposterously large . . . */
  625. #endif /* not MSDOS */
  626.   -1
  627. };
  628.  
  629. /* Index of current nbuckets in primes. */
  630. static int primes_index;
  631.  
  632. /* Find the equiv class associated with line N of the current file.  */
  633.  
  634. static int
  635. find_equiv_class (n)
  636.      int n;
  637. {
  638.   int bucket;
  639.   struct equivclass *b, *p = NULL;
  640.  
  641.   /* Equivalence class 0 is permanently allocated to lines that were
  642.      not hashed because they were parts of identical prefixes or
  643.      suffixes. */
  644.   if (n < current->prefix_lines
  645.       || current->linbuf[n].text >= current->suffix_begin)
  646.     return 0;
  647.  
  648.   /* Check through the appropriate bucket to see if there isn't already
  649.      an equivalence class for this line. */
  650.   bucket = current->linbuf[n].hash % nbuckets;
  651.   b = buckets[bucket];
  652.   while (b)
  653.     {
  654.       if (b->line.hash == current->linbuf[n].hash
  655.       && (b->line.length == current->linbuf[n].length
  656.           /* Lines of different lengths can match with certain options.  */
  657.           || length_varies)
  658.       && !line_cmp (&b->line, ¤t->linbuf[n]))
  659.     return b - equivs;
  660.       p = b, b = b->next;
  661.     }
  662.  
  663.   /* Create a new equivalence class in this bucket. */
  664.  
  665. #ifdef MSDOS
  666.   if (equivs_index >= equivs_alloc)
  667.     fatal ("too many differences, hash table overflow");
  668. #endif /* MSDOS */
  669.  
  670.   p = &equivs[equivs_index++];
  671.   p->next = buckets[bucket];
  672.   buckets[bucket] = p;
  673.   p->line = current->linbuf[n];
  674.  
  675.   return equivs_index - 1;
  676. }
  677.  
  678. /* Given a vector of two file_data objects, read the file associated
  679.    with each one, and build the table of equivalence classes.
  680.    Return nonzero if either file appears to be a binary file.  */
  681.  
  682. int
  683. read_files (filevec)
  684.      struct file_data filevec[];
  685. {
  686.   int i, j;
  687.   int binary = 0;
  688.   int this_binary;
  689.  
  690.   current = &filevec[0];
  691.   binary = this_binary = slurp ();
  692.  
  693.   current = &filevec[1];
  694.   this_binary = slurp ();
  695.   if (binary || this_binary)
  696.     return 1;
  697.  
  698.   find_identical_ends (filevec);
  699.  
  700.   for (i = 0; i < 2; ++i)
  701.     {
  702.       current = &filevec[i];
  703.       find_and_hash_each_line ();
  704.     }
  705.  
  706. #ifdef MSDOS
  707.   /* This is NOT guaranteed to be enough space, but we will try anyway and
  708.      abort iff the hash table really overflows.  The strategy will help
  709.      us A LOT iff there are long matching pre- and suffixes (but the
  710.      user will have to wait longer for the bad news if we have to
  711.      abort ...).  */
  712.   equivs_alloc = min ((int) (0xffe0 / sizeof (struct equivclass)),
  713.         filevec[0].buffered_lines + filevec[1].buffered_lines + 1);
  714. #else /* not MSDOS */
  715.   /* This is guaranteed to be enough space.  */
  716.   equivs_alloc = filevec[0].buffered_lines + filevec[1].buffered_lines + 1;
  717. #endif /* not MSDOS */
  718.  
  719.   equivs = (struct equivclass *) xmalloc (equivs_alloc * sizeof (struct equivclass));
  720.   /* Equivalence class 0 is permanently safe for lines that were not
  721.      hashed.  Real equivalence classes start at 1. */
  722.   equivs_index = 1;
  723.   
  724.   primes_index = 0;
  725.   while (primes[primes_index] < equivs_alloc / 3)
  726.     primes_index++;
  727.  
  728.   buckets = (struct equivclass **) xmalloc (primes[primes_index] * sizeof (struct equivclass *));
  729.   bzero (buckets, primes[primes_index] * sizeof (struct equivclass *));
  730.   nbuckets = primes[primes_index];
  731.  
  732.   for (i = 0; i < 2; ++i)
  733.     {
  734.       current = &filevec[i];
  735.       current->equivs
  736.     = (int *) xmalloc (current->buffered_lines * sizeof (int));
  737.       for (j = 0; j < current->buffered_lines; ++j)
  738.     current->equivs[j] = find_equiv_class (j);
  739.     }
  740.  
  741.   filevec[0].equiv_max = filevec[1].equiv_max = equivs_index;
  742.  
  743.   free (equivs);
  744.   free (buckets);
  745.  
  746.   return 0;
  747. }
  748.